/**
 * 
 */
package com.ckex.learn.lucene.dao.impl;

import java.util.Collections;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.ckex.learn.lucene.dao.TradeDao;
import com.ckex.learn.lucene.dao.page.PageList;
import com.ckex.learn.lucene.dao.page.PageQuery;
import com.ckex.learn.lucene.dao.page.Paginator;
import com.ckex.learn.lucene.dao.page.SearchMap;
import com.ckex.learn.lucene.domain.TradeDo;

/**
 * @author ckex created 2013-10-7 - 下午1:56:45 TradeDaoImpl.java
 * @explain -
 */
@Repository("tradeDao")
public class TradeDaoImpl extends BaseDao implements TradeDao {

    @SuppressWarnings("unchecked")
    @Override
    public PageList<TradeDo> listByPage(Long sellerId, PageQuery pageQuery, Integer count) {

        SearchMap map = new SearchMap();
        map.add("sellerId", sellerId);

        map.put("startIndex", pageQuery.getStartIndex());
        map.put("pageSize", pageQuery.getPageSize());

        if (count == null || count.intValue() == 0) {
            count = (Integer) getSqlMapClientTemplate().queryForObject("t_trade.listByPageCount",
                    map);
        }
        List<TradeDo> list = Collections.emptyList();
        if (count != null && count.intValue() > 0) {
            list = (List<TradeDo>) getSqlMapClientTemplate()
                    .queryForList("t_trade.listByPage", map);
        }

        Paginator paginator = new Paginator(pageQuery.getPageSize(), count);
        paginator.setPage(pageQuery.getPageNum());

        PageList<TradeDo> result = new PageList<TradeDo>(list);
        result.setPaginator(paginator);

        return result;

    }

    @Override
    public TradeDo loadById(Long sellerId, Long id) {
        SearchMap map = new SearchMap();
        map.add("id", id);
        if (sellerId != null) {
            map.add("sellerId", sellerId);
        }
        return (TradeDo) getSqlMapClientTemplate().queryForObject("t_trade.load", map);
    }

}
